In TypeScript, casts and non-null assertions are two mechanisms used to inform the TypeScript compiler about the intended types of variables,
expressions, or values in the code. They are used to help the compiler understand the types more accurately and to handle certain situations where
type inference might not be sufficient:
- A type assertion tells the compiler to treat the value as the specified type, even if the compiler’s type inference suggests a different type.
- A non-null assertion is a way to tell the TypeScript compiler explicitly that you are certain that a variable will not be
null
or
undefined
, and you want to access its properties or methods without any null checks.
However, a redundant cast occurs when you perform a type assertion that is not needed because the compiler already knows the type based on the
context or explicit type declarations. Similarly, a redundant non-null assertion occurs when you use the non-null assertion operator !
to
assert that a variable is not null
or undefined
, but the compiler already knows that based on the context.
Both redundant casts and redundant non-null assertions should be avoided in TypeScript code, as they add unnecessary noise, clutter the code, and
lead to confusion.
function getName(x?: string | Person) {
if (x) {
console.log("Getting name for " + x!); // Noncompliant: 'x' is known to be defined here
if (typeof x === "string") {
return (x as string); // Noncompliant: 'x' is known to be a string here
} else {
return (x as Person).name; // Noncompliant: 'x' is defined and not a string, thus a 'Person' here
}
}
return "NoName";
}
Remove all the redundant casts and non-null assertions based on the contextual typing information, as inferred by the TypeScript compiler.
function getName(x?: string | Person) {
if (x) {
console.log("Getting name for " + x);
if (typeof x === "string") {
return x;
} else {
return x.name;
}
}
return "NoName";
}